Ext.onReady(function() {
	Ext.QuickTips.init();

	setContainerSize();

	setTimeout(function() {
		loadData();
	}, 400);
});

var containerID = "stkosutilVisualizerContainer";

function GetQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if (r != null)
		return unescape(r[2]);
	return null;
}

function setContainerSize() {
	var iframeID = GetQueryString("iframeId");
	
	var iframe = parent.document.getElementById(iframeID);
	var parentNode = iframe.parentNode;

	var width = (parentNode.offsetWidth - 2) + "px";
	var height = (parentNode.offsetHeight - 2) + "px";

	var main = document.getElementById(containerID);

	main.innerHTML = "";
	main.setAttribute("style", "width:" + width + ";height:" + height + ";");
}

function loadData() {
	var projectId = GetQueryString('projectId');

	Ext.Ajax.request({
		url : '/webprotege/stkosutilowlentitytreeservlet',
		method : 'POST',
		params : {
			projectId : projectId
		},
		callback : function(options, success, response) {
			if (success) {
				var json = Ext.util.JSON.decode(response.responseText);
				if (json.success) {
					var result = json.result;

					var iForce = new IForce({
						renderToId : containerID,
						echartspath : "../plugin/echarts",
						data : result
					});

					iForce.draw();

				} else {
					Ext.Msg.show({
						title : 'Error',
						msg : json.msg,
						buttons : Ext.MessageBox.OK,
						icon : Ext.MessageBox.ERROR
					});
				}
			} else {
				Ext.Msg.show({
					title : 'Error',
					msg : 'Loading project class tree failure!',
					buttons : Ext.MessageBox.OK,
					icon : Ext.MessageBox.ERROR
				});
			}
		}
	});
}
